#!/bin/bash
###################
### powerpellet ###
version="0.1"; ####
### OS Hazard #####
###################



######################
# http://projects.archlinux.org/svntogit/packages.git/plain/repos/core-i686/PKGBUILD?h=packages/zlib&id=bf4a5e09cd5287bd037224744d4638f48326fb60
#
# http://projects.archlinux.org/svntogit/packages.git/tree/repos/core-i686?h=packages/zlib&id=bf4a5e09cd5287bd037224744d4638f48326fb60 \
#
# http://projects.archlinux.org/svntogit/packages.git/tree/repos/core-i686/zlib-1.2.5-lfs-decls.patch?h=packages/zlib&id=bf4a5e09cd5287bd037224744d4638f48326fb60
#
# http://projects.archlinux.org/svntogit/packages.git/plain/repos/core-i686/?h=packages/zlib&id=bf4a5e09cd5287bd037224744d4638f48326fb60 \
#
# curl "http://projects.archlinux.org/svntogit/packages.git/plain/repos/core-i686/?h=packages/zlib&id=bf4a5e09cd5287bd037224744d4638f48326fb60" \
#
# cat url | sed -e 's/></>\n</g' -e 's/<\/a>//g' -e 's/>//g' -e 's/\&amp;/\&/g'  | grep "^<a href=" | grep -v "../$" | awk -F "packages.git" '{print $2}' | awk -F "'" '{print $1}'
######################


saveDir="/opt/pellet";
pkgname="$1";
arch=$(uname -m);

if [ -z "$pkgname" ];
then echo "Missing parameter"; echo "USAGE: powerpellet <package name>"; exit 1;
fi

if [ ! -d "$saveDir" ];
then 
  echo "Creating save directory ...";
  mkdir -p "$saveDir" 2>/dev/null;
  chmod -R 777 "$saveDir" 2>/dev/null;
  if [ ! -d "$saveDir" ];
  then 
    sudo mkdir -p "$saveDir" 2>/dev/null;
    sudo chmod -R 777 "$saveDir" 2>/dev/null;
    if [ ! -d "$saveDir" ];
    then echo "Permission denied"; exit 1;
    fi;
  fi;
fi;

save_dir()
{
  if [ ! -d "$saveDir/$pkgname/$selection" ];
  then mkdir -p "$saveDir/$pkgname/$selection"; 
    if [ -d "$saveDir/$pkgname/$selection" ];
    then chmod -R 777 "$saveDir/$pkgname";
    else exit 1;
    fi;
  fi;
}


echo -n "Searching ...";
repo=$(pacman -Ss "$pkgname" | grep "/$pkgname " | awk -F "/" '{print $1}');

if [ ! -z "$repo" ];
then echo -e "\t[DONE]";
else echo -e "\t[FAIL]"; echo "Package not found"; exit 1;
fi

echo -n "Downloading ...";
link_list=$(curl -s http://projects.archlinux.org/svntogit/packages.git/log/repos/$repo-$arch/PKGBUILD?h=packages/$pkgname | grep -e "archrelease: copy" -e "db-move: moved" | grep -v "moved $pkgname from \[$repo\]" | sed 's/></\n/g' | grep "^a href" | awk -F "'" '{print $2}' | sed -e 's/^/http:\/\/projects.archlinux.org/g' -e 's/\&amp;/\&/g' -e 's/packages.git\/commit/packages.git\/plain/');
echo -e "\t[DONE]";

echo "Parsing available ...";
num="0";
for link in $link_list;
do
  num=$((num+1));
  release=$(curl -s "$link" | grep -e "^pkgver=" -e "^pkgrel=" | awk -F "=" '{print $2}'); 
  release=$(echo $release | sed -e 's/\./_/g' -e 's/ /__/');
  declare rel_${release}="$link";
  if [ "$num" -lt "10" ]; then spacer=" "; else unset spacer; fi;
  echo -e "$spacer$num.) $repo/$pkgname $release" | sed -e 's/__/-/g' -e 's/_/\./g';
  rel_list="$rel_list\n $release";
done

if [ "$num" = "0" ];
then echo "[BUG DETECTED]: Parsing error found."; echo "Please report bug to <oshazard+powerpellet@gmail.com>"; exit 1;
else rel_list=$(echo -e "$rel_list" | sed -e '1 d' -e 's/ //g');
fi

echo "";
read -p "Input numeric selection [1-$num]: " input;
if [ "$input" -ge "1" -a "$input" -le "$num" ] 2>/dev/null;
then release=$(echo "$rel_list" | awk -v line=$input NR==line);
else echo "Invalid selection"; exit 1;
fi

url=$(eval echo "\$rel_$release");
selection=$(echo $release | sed -e 's/__/-/g' -e 's/_/\./g');
echo "$url";
read -p "Download: $repo/$pkgname $selection (y/N)?: " download;
if [ "$download" = "Y" -o "$download" = "y" ];
then 
  save_dir; 
  wget -q "$url" -O "$saveDir/$pkgname/$selection/PKGBUILD"; 
  wget -q "$url" -O "$saveDir/$pkgname/$selection/$pkgname.tar.gz"; 
  cp "$saveDir/$pkgname/$selection/PKGBUILD" "$saveDir/$pkgname";
  cp "$saveDir/$pkgname/$selection/$pkgname.tar.gz" "$saveDir/$pkgname";
  echo "$saveDir/$pkgname/";
fi;
